﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace ShadowGame
{
    public class Player : GameComponent
    {
        private float moveRate = 200.0f;
        private Vector2 initialPosition = new Vector2(0, 450);
        //private Vector2 initialPosition = new Vector2(100, BackgroundManager.Height);
        /*public bool jumping;
        public bool falling;
        private float jumpSpeed;
        private float startY;*/
        private Directions directions;
        private bool drawDirections = false;
        private List<Clue> clues;
        private List<Brick> bricks;
        ContentManager content;
        Vector2 startingPosition = Vector2.Zero;
        private string animation = "idle";
        private Vector2 fallSpeed = new Vector2(0, 20);

        public Player(ContentManager content, List<Brick> bricks) : base(bricks)
        {
            this.content = content;
            this.bricks = bricks;
            clues = new List<Clue>();
            Texture2D player = content.Load<Texture2D>(@"Images\player");
            directions = new Directions(content);
            animations.Add("idle", new Animation(
                player,
                100,
                150,
                new Rectangle(0, 301, 100, 150),
                1,
                "idle"
                ));
            animations["idle"].LoopAnimation = true;

            animations.Add("walkLeft", new Animation(
                player,
                100,
                150,
                new Rectangle(0, 151, 100, 150),
                5,
                "walkLeft"));
            animations["walkLeft"].LoopAnimation = true;
            animations["walkLeft"].FrameLength = 0.15f;

            animations.Add("walkRight", new Animation(
                player,
                100,
                150,
                new Rectangle(0, 0, 100, 150),
                5,
                "walkRight"));
            animations["walkRight"].LoopAnimation = true;
            animations["walkRight"].FrameLength = 0.15f;

            frameWidth = 100;
            frameHeight = 150;
            drawDepth = 0.825f;
            enabled = true;
            PlayAnimation("idle");

            worldPosition = initialPosition;
            CollisionRectangle = new Rectangle(0, 0, 70, 150);
        }

        public bool DrawDirections { get { return drawDirections; }}

        public override void Update(GameTime gameTime)
        {
            string newAnimation = "idle";
            velocity = new Vector2(0, velocity.Y);
            
            KeyboardState keyState = Keyboard.GetState();

            if (keyState.IsKeyDown(Keys.Left) && !keyState.IsKeyDown(Keys.Space))
            {
                flipped = false;
                newAnimation = "walkLeft";
                velocity = new Vector2(-moveRate, velocity.Y);
            }

            if (keyState.IsKeyDown(Keys.Right) && !keyState.IsKeyDown(Keys.Space))
            {
                flipped = false;
                newAnimation = "walkRight";
                velocity = new Vector2(moveRate, velocity.Y);
            }
            if (newAnimation != currentAnimation)
            {
                PlayAnimation(newAnimation);
            }

            if (keyState.IsKeyDown(Keys.Up) && !keyState.IsKeyDown(Keys.Space))
            {
                if (onGround)
                {
                    Jump();
                    newAnimation = "jump";
                }
            }

            if (keyState.IsKeyDown(Keys.Space))
            {
                directions.Update(gameTime);
                directions.Enabled = true;
                drawDirections = true;
            }

            repositionCamera();

            if (keyState.IsKeyUp(Keys.Space))
            {
                directions.Enabled = false;
                if (drawDirections)
                {
                    Clue clue = new Clue(content, directions.CurrentAnimation);
                    clue.WorldPosition = new Vector2(worldPosition.X + WorldRectangle.Width / 2, worldPosition.Y + WorldRectangle.Height - 10);
                    clue.Direction.WorldPosition = new Vector2(worldPosition.X + WorldRectangle.Width / 2, worldPosition.Y - clue.Direction.WorldRectangle.Height);
                    //clue.Direction.WorldPosition = clue.WorldPosition;
                    clue.Enabled = true;
                    clues.Add(clue);
                    drawDirections = false;
                }
            }
            
            directions.WorldPosition = new Vector2(worldPosition.X, worldPosition.Y - directions.WorldRectangle.Height);

            foreach (Clue clue in clues)
            {
                if (!clue.CollisionRectangle.Intersects(CollisionRectangle))
                    clue.FirstIntersect = false;

                if (clue.CollisionRectangle.Intersects(CollisionRectangle) && !clue.FirstIntersect)
                {
                    clue.Direction.Enabled = true;
                    clue.Enabled = false;
                    if (clue.Started < 0)
                        clue.Started = (int)gameTime.TotalGameTime.TotalMilliseconds;
                }

                if (clue.Direction.Enabled)
                    clue.Update(gameTime);
            }
            velocity += fallSpeed;
            base.Update(gameTime);
        }

        private void repositionCamera()
        {
            int screenPosX = (int)Camera.WorldToScreen(WorldPosition).X;
            int screenPosY = (int)Camera.WorldToScreen(WorldPosition).Y;

            if (screenPosX > 500)
            {
                Camera.Move(new Vector2(screenPosX - 500, 0));
            }

            if (screenPosX < 200)
            {
                Camera.Move(new Vector2(screenPosX - 200, 0));
            }

            if (screenPosY > 400)
            {
                Camera.Move(new Vector2(0, screenPosY - 400));
            }

            if (screenPosY < 100)
            {
                Camera.Move(new Vector2(0, screenPosY - 100));
            }

        }

        public void Jump()
        {
            velocity.Y = -650;
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            if (directions.Enabled)
                directions.Draw(spriteBatch);

            foreach (Clue clue in clues)
            {
                clue.Draw(spriteBatch);
            }
            base.Draw(spriteBatch);
        }
    }
}
